package com.kingdee.pda.ui.query_replenish_order;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.basecommon.http.MyObserver;
import com.example.basecommon.http.RxHelper;
import com.example.basecommon.http.rxjava.BaseResponse;
import com.example.basecommon.http.rxjava.HttpUtils;
import com.example.basecommon.utils.ConstantUtil;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.Api;
import com.kingdee.pda.R;
import com.kingdee.pda.adapter.ReplenishOrderDetailAdapter;
import com.kingdee.pda.entity.ReplenishOrderBean;
import com.kingdee.pda.entity.ReplenishOrderDetailBean;
import com.kingdee.pda.entity.ReplenishOrderDetailBean.DataBean.AllotsBean;
import com.kingdee.pda.entity.ReplenishOrderDetailBean.DataBean.ItemsBean;
import com.kingdee.pda.entity.ReplenishOrderDetailBean.DataBean.PutawaysBean;
import com.kingdee.pda.priority.PriorityUtils;
import com.kingdee.pda.ui.activity.BaseActivity;
import com.kingdee.pda.ui.inner_replenish.InnerReplenishActivity;
import com.kingdee.pda.ui.dialog.RemindDialog;
import com.kingdee.pda.util.FastClickUtil;
import com.kingdee.pda.util.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.kingdee.pda.util.WarehouseUtil.getCurrentWarehouse;

public class ReplenishOrderDetailActivity extends BaseActivity implements View.OnClickListener, ReplenishOrderDetailAdapter.OnSkuItemClickListener {

    private ListView mListView;
    private String billNo;
    private ReplenishOrderDetailBean.DataBean data;
    private TextView tv_orderNo;
    private TextView tv_status;
    private TextView tv_type;
    private TextView tv_moveOrder;
    private TextView tv_replenishRemake;
    private TextView tv_skuTypeNumber;
    private TextView tv_replenishNumber;
    private TextView tv_updateTime;
    private int actualQty = 0;
    private int wantQty = 0;
    private ReplenishOrderDetailAdapter replenishOrderSkuDetailAdapter;
    private Button btn_replenishStart;

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_replenish_order_detail);
    }

    @Override
    protected void initView() {

        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.tv_moveOrderDetail).setOnClickListener(this);
        findViewById(R.id.tv_remakeDetail).setOnClickListener(this);
        btn_replenishStart = findViewById(R.id.btn_replenishStart);
        btn_replenishStart.setOnClickListener(this);
        if (PriorityUtils.INSTANCE.getStartreplenishment()){
            btn_replenishStart.setVisibility(View.VISIBLE);
        }
        tv_orderNo = findViewById(R.id.tv_orderNo);
        tv_status = findViewById(R.id.tv_status);
        tv_type = findViewById(R.id.tv_type);
        tv_moveOrder = findViewById(R.id.tv_moveOrder);
        tv_replenishRemake = findViewById(R.id.tv_replenishRemake);
        tv_skuTypeNumber = findViewById(R.id.tv_skuTypeNumber);
        tv_replenishNumber = findViewById(R.id.tv_replenishNumber);
        tv_updateTime = findViewById(R.id.tv_updateTime);

        mListView = findViewById(R.id.list_view);
        replenishOrderSkuDetailAdapter = new ReplenishOrderDetailAdapter(this, this);
        mListView.setAdapter(replenishOrderSkuDetailAdapter);

        billNo = getIntent().getStringExtra("billNo");
    }

    @Override
    protected void initData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("warehouseId", getCurrentWarehouse().getId());
        map.put("billNo", billNo);
        MyObserver<ReplenishOrderDetailBean.DataBean> myObserver = new MyObserver<ReplenishOrderDetailBean.DataBean>(this) {
            @Override
            public void onSuccess(BaseResponse<ReplenishOrderDetailBean.DataBean> result) {
                data = result.data;
                reflushViewData();
            }

            @Override
            public void onFailure(BaseResponse result, String msg) {
                SpeakUtils.speakWithToast(ReplenishOrderDetailActivity.this, msg, false, false);
            }
        };
        HttpUtils.request(Api.class)
                .getRepleOrderDetail(ConstantUtil.REPLENISH_GETORDERDETAIL, map)
                .compose(RxHelper.observableIO2Main(this))
                .subscribe(myObserver);


    }

    private void reflushViewData() {
        try {
            ReplenishOrderDetailBean.DataBean.ListVoBean listVo = data.getListVo();
            List<ItemsBean> items = data.getItems();

            tv_orderNo.setText("补货单号：" + listVo.getBillNo());
            if ("A".equals(listVo.getType())) {
                tv_type.setText("补货类型：出库补货");
            } else if ("B".equals(listVo.getType())) {
                tv_type.setText("补货类型：库内补货");
            } else {
                tv_type.setText("补货类型：-");
            }

            tv_moveOrder.setText("移库单号：" + listVo.getMoveBillNos());
            tv_replenishRemake.setText("补货备注：" + listVo.getRemark());
            tv_updateTime.setText("修改时间：" + listVo.getModifyDate());
            tv_skuTypeNumber.setText("SKU种类数：" + items.size());
            if (ReplenishOrderBean.STATUS_WAIT_REPLENISH.equals(listVo.getBillStatus())) {
                tv_status.setText("待补货");
                tv_status.setTextColor(getResources().getColor(R.color.color_01d386));
                tv_status.setBackgroundResource(R.drawable.shape_bg_white_green_side);
            } else if (ReplenishOrderBean.STATUS_IN_REPLENISH.equals(listVo.getBillStatus())) {
                tv_status.setText("补货中");
                tv_status.setTextColor(getResources().getColor(R.color.color_01d386));
                tv_status.setBackgroundResource(R.drawable.shape_bg_white_green_side);
            } else if (ReplenishOrderBean.STATUS_FINISHED_REPLENISH.equals(listVo.getBillStatus())) {
                tv_status.setText("补货完成");
                tv_status.setTextColor(getResources().getColor(R.color.color_01d386));
                tv_status.setBackgroundResource(R.drawable.shape_bg_white_green_side);
            } else if ("A".equals(listVo.getBillStatus()) || "C".equals(listVo.getBillStatus())) {
                tv_status.setText("待提交");
                tv_status.setTextColor(getResources().getColor(R.color.color_FC8555));
                tv_status.setBackgroundResource(R.drawable.shape_bg_white_orange_side);
            } else if ("B".equals(listVo.getBillStatus())) {
                tv_status.setText("配货中");
                tv_status.setTextColor(getResources().getColor(R.color.color_01d386));
                tv_status.setBackgroundResource(R.drawable.shape_bg_white_green_side);
            } else if ("G".equals(listVo.getBillStatus())) {
                tv_status.setText("已取消");
                tv_status.setTextColor(getResources().getColor(R.color.color_01d386));
                tv_status.setBackgroundResource(R.drawable.shape_bg_white_gray_side2);
            }

            wantQty = 0;
            actualQty = 0;
            for (ItemsBean itemBean : items) {
                wantQty += itemBean.getWantQty();
                actualQty += itemBean.getRealQty();
            }

            tv_replenishNumber.setText("期望/实际补货量：" + wantQty + "/" + (actualQty == 0 ? "-" : actualQty));
            replenishOrderSkuDetailAdapter.setData(items);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSkuItemClick(int position) {
        try {
            ItemsBean itemsBean = data.getItems().get(position);
            List<PutawaysBean> putaways = new ArrayList<>();
            List<AllotsBean> allots = new ArrayList<>();
            for (PutawaysBean bean : data.getPutaways()) {
                if (itemsBean.getItemskuId() == bean.getItemskuId()) {
                    putaways.add(bean);
                }
            }
            for (AllotsBean bean : data.getAllots()) {
                if (itemsBean.getItemskuId() == bean.getItemskuId()) {
                    allots.add(bean);
                }
            }

            Intent intent = new Intent(this, ReplenishSkuDetailActivity.class);
            intent.putExtra("itemsBean", itemsBean);
            intent.putExtra("putaways", (Serializable) putaways);
            intent.putExtra("allots", (Serializable) allots);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        if (FastClickUtil.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_moveOrderDetail:
                if (data != null && data.getListVo() != null) {
                    RemindDialog remindDialog = new RemindDialog(this);
                    remindDialog.setTitle("移库单号");
                    remindDialog.setContent(data.getListVo().getMoveBillNos());
                    remindDialog.show();
                }
                break;
            case R.id.tv_remakeDetail:
                if (data != null && data.getListVo() != null) {
                    RemindDialog remindDialog2 = new RemindDialog(this);
                    remindDialog2.setTitle("补货备注");
                    remindDialog2.setContent(data.getListVo().getRemark());
                    remindDialog2.show();
                }
                break;
            case R.id.btn_replenishStart:
                if (data != null && data.getListVo() != null) {
                    //当单据状态=【待补货、补货中】，且补货情况=空/部分补货，可点击；
                    if ((ReplenishOrderBean.STATUS_WAIT_REPLENISH.equals(data.getListVo().getBillStatus())
                            || ReplenishOrderBean.STATUS_IN_REPLENISH.equals(data.getListVo().getBillStatus()))
                            && (TextUtils.isEmpty(data.getListVo().getReplenishStatus()) || "A".equals(data.getListVo().getReplenishStatus()))) {
                        Intent intent = new Intent(this, InnerReplenishActivity.class);
                        intent.putExtra("billNo", billNo);
                        startActivity(intent);
                        finish();
                    } else {
                        SpeakUtils.speakWithToast(this, "仅未完成补货时允许补货!", false, false);
                    }
                }
                break;
        }
    }

    @Override
    protected void onNetworkAvailable() {

    }

    @Override
    protected void onNetworkError() {
        ToastUtils.showLongOnDeepBg(ReplenishOrderDetailActivity.this, "网络连接失败");
    }
}
